import vue from '@vitejs/plugin-vue';
import styleImport from 'vite-plugin-style-import';
const { resolve } = require('path');
import { defineConfig } from 'vite';
import eslintPlugin from 'vite-plugin-eslint'; // 引入vite-plugin-vue
import viteCompression from 'vite-plugin-compression';
export default defineConfig({
  base: '/',
  resolve: {
    alias: [{ find: '@', replacement: resolve(__dirname, 'src') }],
  },
  server: {
    // proxy: {
    //   '/api': {
    //     target: 'xxxx',
    //     changeOrigin: true,
    //     rewrite: path => path.replace(/^\/api/, '/'),
    //   },
    // },
  },
  css: {
    preprocessorOptions: {
      scss: { charset: false, additionalData: `@use "./src/assets/css/index.scss" as *;` },
    },
  },
  build: {
    assetsDir: './static',
    chunkSizeWarningLimit: 500,
    minify: 'terser',
    cssCodeSplit: true, // 如果设置为false，整个项目中的所有 CSS 将被提取到一个 CSS 文件中
    terserOptions: {
      compress: {
        // warnings: false,
        drop_console: true,
        drop_debugger: true,
        pure_funcs: ['console.log'],
      },
      output: {
        // 去掉注释内容
        comments: true,
      },
    },
    rollupOptions: {
      output: {
        manualChunks: {
          // 拆分代码
          vue: ['vue', 'vue-router', 'vuex'], // 如果打包有BUG请屏蔽
          vant: ['vant'],
          echarts: ['echarts'],
          // echarts: ['echarts'],
        },
      },
    },
    brotliSize: false,
  },
  plugins: [
    vue(),
    // 打包压缩
    viteCompression(),
    // eslint检查文件跑配置
    eslintPlugin({
      include: ['src/**/*.vue', 'src/**/*.js'], // 检查的文件
    }),
    // vant按需引入
    styleImport({
      libs: [
        {
          libraryName: 'vant',
          esModule: true,
          resolveStyle: name => `vant/es/${name}/style`,
        },
      ],
    }),
  ],
});
